using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Skins.Info;
using System.Reflection;
using System.Drawing;
using System.IO;

namespace DevExpress.Skins {
    public class SingleSkinXmlCreator : SkinXmlCreator {
        string[] validImageNames = null;
        public SingleSkinXmlCreator(string skinName, string relativePath) : this(null, skinName, relativePath, null, null) { }
        public SingleSkinXmlCreator(string[] validImageNames, string skinName, string relativePath, Assembly skinAssembly, string skinXml)
            : base(skinName, relativePath, skinAssembly, skinXml) {
            this.validImageNames = validImageNames;
        }
        public override SkinCreator Clone(string skinName) { return new SingleSkinXmlCreator(this.validImageNames, skinName, RelativePath, SkinAssembly, SkinXml); }
        public override void Load() {
            SetLoaded();
            using(Stream stream = SkinAssembly.GetManifestResourceStream(RelativePath + SkinXml)) {
                SkinXmlLoader loader = new SkinXmlLoader(this);
                loader.LoadAllSkins(stream, SkinName);
            }
        }

        Bitmap empty = new Bitmap(1, 1);
        protected override Image LoadImage(SkinImage image, string imageName) {
            if(validImageNames != null && !IsValidImage(imageName)) return empty;

            try {
                return base.LoadImage(image, imageName);
            }
            catch {
                return empty;
            }
        }

        bool IsValidImage(string imageName) {
            foreach(string s in validImageNames) {
                if(imageName.EndsWith(s)) return true;
            }
            return false;
        }
    }
}
